package com.liuyu.common.framework.generator.plus;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.builder.ConfigBuilder;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 * @ClassName MyAutoGenerator
 * @Author: liuyu
 * @Date: Created in 2020/4/4 19:37
 * @Version: 1
 * @Modified By:
 */
public class MyAutoGenerator extends AutoGenerator {


    public MyAutoGenerator(@NotNull DataSourceConfig dataSourceConfig) {
        super(dataSourceConfig);
    }

    @Override
    protected List<TableInfo> getAllTableInfoList(ConfigBuilder config) {
        List<TableInfo> tableInfos = super.getAllTableInfoList(config);
        tableInfos.forEach(t -> {
            t.getFields().forEach(f -> {
                if (StringUtils.isNotBlank(f.getComment())) {
                    String comment = f.getComment();
                    comment = comment.replaceAll("\r\n", "");//注意，替换换行符
                    if (comment.contains("\n")) {
                        comment = comment.replace("\n", "");
                    }
                    if (comment.contains(" ")) {
                        comment = comment.replace(" ", "");
                    }
                    f.setComment(comment);
                } else {
                    //如果没有注释，则直接使用字段名称
                    f.setComment(f.getName());
                }
            });
        });
        return tableInfos;
    }
}
